<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.chart_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ext_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:chart:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:chart:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:chart:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.chart_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name">
        <template slot-scope="scope">
          <router-link :to="'/chart/view/?id=' + scope.row.id" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="数据源" align="center" prop="ds_code" />
      <el-table-column label="搜索标题" align="center" prop="search_title" />
      <el-table-column label="搜索类型" align="center" prop="search_type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.chart_search_type" :value="scope.row.search_type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ext_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:chart:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:chart:remove']"
          >删除</el-button>
          <el-button v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleMenu(scope.row)"
            v-hasPermi="['system:menu:add']"
          >菜单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改自定义报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-tabs v-model="tab">
        <el-tab-pane label="任务信息" name="base">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.chart_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="数据源" prop="ds_code">
          <el-select v-model="form.ds_code" placeholder="请选择">
            <el-option
              v-for="dict in dsOptions"
              :key="dict.value"
              :label="dict.label + ' ' + dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据解析" prop="data_option">
          <el-input v-model="form.data_option" type="textarea" rows="6" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.ext_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="搜索配置" name="search">
        <el-form-item label="搜索类型" prop="search_type">
          <el-select v-model="form.search_type" placeholder="请选择搜索类型">
            <el-option
              v-for="dict in dict.type.chart_search_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="搜索标题" prop="search_title">
          <el-input v-model="form.search_title" placeholder="请输入搜索标题" />
        </el-form-item>
        <el-form-item label="搜索字段" prop="search_field">
          <el-input v-model="form.search_field" placeholder="请输入搜索字段" />
        </el-form-item>
        <el-form-item label="搜索来源" prop="search_ds" v-if="form.search_type == 'tree' || form.search_type == 'ds'">
          <el-select v-model="form.search_ds" placeholder="请选择">
            <el-option
              v-for="dict in dsOptions"
              :key="dict.value"
              :label="dict.label + ' ' + dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="搜索字典" prop="search_ds" v-if="form.search_type == 'dict'">
          <el-select v-model="form.search_ds" placeholder="请选择">
            <el-option
              v-for="dict in dictOptions"
              :key="dict.value"
              :label="dict.label + ' ' + dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-tab-pane>
      </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-dialog :open="upload" model="dev_chart" @cancel="importCancel" @success="importSuccess" />
  </div>
</template>

<script>
import { addMenu } from "@/api/system/menu";
import { apiGet, apiPost, apiPut, apiDelete, apiDs, apiExport, apiDownload } from "@/api/openapi";
import ImportDialog from "@/components/ImportDialog";

export default {
  name: "Chart",
  components: {
    ImportDialog
  },
  dicts: ['chart_type', 'ext_status', 'chart_search_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自定义报表格数据
      chartList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        type: null,
        name: null,
        ds_code: null,
        data_option: null,
        search_title: null,
        search_field: null,
        search_type: null,
        search_ds: null,
        status: null,
        "@alias": "t",
        "@exp": {
          name: 'lk',
        }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      tab: 'base',
      dsOptions:[],
      dictOptions: [],
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        ds_code: [
          { required: true, message: "数据源不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    apiGet({'@pager': {limit:1000}, "@table":"api_sql", "type":["chart","table"], "@exp":{"type":"in"}, "@column":"code:value,name:label"}).then(response => {
      this.dsOptions = response.data.rows;
    });
    apiGet({'@pager': {limit:1000}, "@table":"sys_dict_type", "status":'0', "@column":"dict_type:value,dict_name:label"}).then(response => {
      this.dictOptions = response.data.rows;
    });
  },
  methods: {
    /** 查询自定义报列表 */
    getList() {
      this.loading = true;
      let params = {'@pager': this.pager};
      params['dev_chart'] = {...this.queryParams};
      apiGet(params).then(response => {
        this.chartList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        ds_code: null,
        data_option: null,
        search_title: null,
        search_field: null,
        search_type: null,
        search_ds: null,
        status: "1"
      };
      this.resetForm("form");
      this.tab = 'base';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自定义报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {};
        data['dev_chart'] = { id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自定义报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
        let data = {
          'dev_chart': this.form,
           '@tags': ['dev_chart']
         };
          if (this.form.id != null) {
            apiPut(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            apiPost(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 添加菜单按钮 */
    handleMenu(row) {
      let data = {
        menuId: undefined,
        parentId: 2200,
        menuName: row.name,
        remark: row.name,
        icon: 'chart',
        menuType: "C",
        orderNum: 9,
        path: row.ds_code,
        perms:'chart:view:'+row.id,
        component:'chart/index',
        query:'{"id":'+row.id+'}',
        isFrame: "1",
        isCache: "0",
        visible: "0",
        status: "0"
      };

      addMenu(data).then(response => {
        this.$modal.msgSuccess("添加菜单成功");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的自定义报数据？').then(function() {
        let data = {};
        data['dev_chart'] = { id:ids, "@exp":{id:"in"}};
        data['@tags'] = ['dev_chart'];
        return apiDelete(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload = true;
    },
    importSuccess(msg) {
        this.upload = false;
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
    },
    importCancel() {
        this.upload = false;
    },
    /** 导出按钮操作 */
    handleExport() {
        let params = {};
      params['dev_chart'] = {...this.queryParams};
      params['@pager'] = {page:1, limit:10000};
      apiExport(params).then(response => {
        apiDownload(response.msg);
      });
    }
  }
};
</script>
